package client

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/client"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var RechargeGearGroup = core.Group("/client", newGearHandler, regGear, middleware.TokenAuth())

func newGearHandler(srv client.IRechargeGearService) *gearHandler {
	return &gearHandler{srv: srv}
}

func regGear(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *gearHandler) {
		rg.GET("/recharge_gear/list", handle.list)
		rg.GET("/recharge_gear/detail", handle.detail)
		rg.POST("/recharge_gear/add", handle.add)
		rg.POST("/recharge_gear/edit", handle.edit)
		rg.POST("/recharge_gear/del", handle.del)
		rg.GET("/recharge_gear/sign", handle.sign)
	})
}

type gearHandler struct {
	srv client.IRechargeGearService
}

// list gear列表
func (hd gearHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.RechargeGearListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail gear详情
func (hd gearHandler) detail(c *gin.Context) {
	var detailReq req.RechargeGearDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.Id)
	response.CheckAndRespWithData(c, res, err)
}

// add gear新增
func (hd gearHandler) add(c *gin.Context) {
	var addReq req.RechargeGearAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(addReq))
}

// edit gear编辑
func (hd gearHandler) edit(c *gin.Context) {
	var editReq req.RechargeGearEditReq

	fmt.Println(fmt.Sprintf("editReq.Price: %v", editReq.Price))

	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

// del gear删除
func (hd gearHandler) del(c *gin.Context) {
	var delReq req.RechargeGearDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.Id))
}

func (hd gearHandler) sign(c *gin.Context) {
	res, err := hd.srv.Sign()

	response.CheckAndRespWithData(c, res, err)
}
